package com.famiover.generator.plugins;

import com.famiover.generator.utils.BlogGeneratorConstant;
import com.famiover.generator.utils.Utils;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

public class MapperJavaPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        boolean extend = Utils.isExtend(context);
        String mapperPath = Utils.getPropertyDefault(context, "mapperPath", "");
        if (Utils.isNotEmpty(mapperPath)) {
            addAnnotation(mapperPath, interfaze);
        }
        Utils.mapperJavaExtendBase(interfaze, topLevelClass, introspectedTable);
        if (!extend) {
            Utils.addMapperExampleMethod(BlogGeneratorConstant.SelectByExampleSelective, "example", interfaze, introspectedTable);
            Utils.addMapperExampleMethod(BlogGeneratorConstant.CountByExampleSelective, "example", interfaze, introspectedTable);
            Utils.addMapperModelMethod(BlogGeneratorConstant.InsertBatchSelective, "records", interfaze, introspectedTable);
            Utils.addMapperModelMethod(BlogGeneratorConstant.UpdateBatchByPrimaryKeySelective, "records", interfaze, introspectedTable);
            Utils.addMapperExampleMethod(BlogGeneratorConstant.SelectByPrimaryKey, Utils.getPKColumnName(introspectedTable), interfaze, introspectedTable);
        }
        Utils.addAuthor(interfaze);
        return true;
    }

    @Override
    public boolean clientDeleteByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }

    @Override
    public boolean clientCountByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }


    @Override
    public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }

    @Override
    public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        boolean extend = Utils.isExtend(context);
        if (!extend) {
            Utils.addMapperExampleMethod(BlogGeneratorConstant.UpdateByExampleSelective, "example", interfaze, introspectedTable);
        }
        return false;
    }

    @Override
    public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }

    @Override
    public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        boolean extend = Utils.isExtend(context);
        if (!extend) {
            Utils.addMapperExampleMethod(BlogGeneratorConstant.UpdateByExample, "example", interfaze, introspectedTable);
        }
        return false;
    }

    private void addAnnotation(String annotationPath, Interface interfaze) {
        FullyQualifiedJavaType qualifiedJavaType4 = new FullyQualifiedJavaType(annotationPath);
        interfaze.addImportedType(qualifiedJavaType4);
        final String addAnnotation = Utils.getBaseName(annotationPath);
        interfaze.addAnnotation("@" + addAnnotation);
    }


}
